package com.zx.map.service.impl;

import com.zx.map.dao.MessageSendLogDao;
import com.zx.map.dao.model.MessageSendLog;
import com.zx.map.dao.model.MessageSendLogExample;
import com.zx.map.service.MessageSendLogService;
import com.zx.map.service.model.MessageSendLogModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("messageSendLogService")
public class MessageSendLogServiceImpl implements MessageSendLogService {

    @Autowired
    private MessageSendLogDao dao;


    /**
     * 验证验证码是否正确
     *
     * @param telephone
     * @param verifyCode
     * @return
     */
    public boolean checkVerifyCode(String telephone, String verifyCode, String messageType) {
        // 验证 验证码是否合法
        MessageSendLogModel paramMessage = new MessageSendLogModel();
        paramMessage.setVerifyCode(verifyCode);
        paramMessage.setMessageType(messageType);//增加短信类型
        paramMessage.setTelphone(telephone);
        List<MessageSendLogModel> list = getListByModel(paramMessage);
        if (null != list && list.size() >= 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据Model查找列表
     */
    public List<MessageSendLogModel> getListByModel(MessageSendLogModel model) throws DataAccessException {
        MessageSendLogExample example = new MessageSendLogExample();
        MessageSendLogExample.Criteria criteria = example.createCriteria();
        criteria.andExpireDateGreaterThan(new Date());//查询系统当前时间仍然生效的数据
        if (StringUtils.isNotBlank(model.getTelphone())) {
            criteria.andTelphoneEqualTo(model.getTelphone());
        }
        if (StringUtils.isNotBlank(model.getVerifyCode())) {
            criteria.andVerifyCodeEqualTo(model.getVerifyCode());
        }
        if (StringUtils.isNotBlank(model.getMessageType())) {
            criteria.andMessageTypeEqualTo(model.getMessageType());
        }
        List<MessageSendLog> list = dao.getListByExample(example);
        List<MessageSendLogModel> modelList = new ArrayList<MessageSendLogModel>();
        for (MessageSendLog o : list) {
            MessageSendLogModel temp = new MessageSendLogModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

    /**
     * 新增
     */
    public String add(MessageSendLogModel model) throws DataAccessException {
        MessageSendLog temp = new MessageSendLog();
        BeanUtils.copyProperties(model, temp);
        String key = dao.add(temp);
        return key;
    }

    /**
     * 修改
     */
    public String update(MessageSendLogModel model) throws DataAccessException {
        MessageSendLog temp = new MessageSendLog();
        BeanUtils.copyProperties(model, temp);
        dao.update(temp);
        return model.getVerifyCode();
    }
}
